package com.example.g32

import android.os.Bundle
import android.os.Handler
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.adapters.TextViewBindingAdapter.setText
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvi.BaseViewActivity
import com.example.g32.databinding.ActivitySpiashBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask
import kotlin.concurrent.schedule
import kotlin.concurrent.timerTask

@AndroidEntryPoint
@Route(path = "/app/SpiashActivity")
class SpiashActivity : BaseViewActivity<ActivitySpiashBinding>() {
    var timer : Timer?=null
    var pro = 5
    override val binding by lazy {
        ActivitySpiashBinding.inflate(layoutInflater)
    }

    override fun initView() {
        val inTime = SPUtils.getInstance().getLong("inTime", 0L)
//        val time = (System.currentTimeMillis() - inTime) / 1000 / 60 / 24 / 7
//        if (time<=7){
//            ARouter.getInstance().build("/app/LoginActivity").navigation()
//            finish()
//        }


        val aa = 5*60*1000
        if ((System.currentTimeMillis() - inTime)<aa){
            ARouter.getInstance().build("/app/LoginActivity").navigation()
            ToastUtils.showLong("登陆过了")
            finish()
        }
        lifecycleScope.launch(Dispatchers.IO) {
            timer = Timer()
            timer?.schedule(timerTask {
                lifecycleScope.launch (Dispatchers.Main){
                    binding.djs.setText("${pro--}s")
                    if (pro == 0){
                        start()
                    }
                }
            },0,1000)
        }
        binding.skip.setOnClickListener {
           start()
        }
    }
    fun start(){
        SPUtils.getInstance().put("inTime",System.currentTimeMillis())
        timer!!.cancel()
        ARouter.getInstance().build("/app/LoginActivity").navigation()
    }
    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        if (timer != null){
            timer?.cancel()
            timer= null
        }
    }
}